figure(56)
%% Q2 data: '20230514_100VR_30G_10kHz_REMPI_scan_3'
x2 = [-160  -120   -80   -40   -30   -20   -10     0     5    10    15    20    25    30    40    50];
signal = [7.2500   10.8500   13.0000   22.5500   24.0500   28.2000   32.6000   40.9000   47.5000   50.6000   53.0000   56.1000   50.8000   40.8500   16.4500    3.5500];
e_signal = [0.6500    0.8016    0.8775    1.0897    1.1391    1.2207    1.3077    1.4612    1.5732    1.6155    1.6553    1.6956    1.6294    1.4517    0.9341    0.5025];

bk = [2.5500    1.9000    0.8500    3.8000    3.3000    3.2500    4.0500    4.8000    3.4000    4.2500    4.1000    4.4500    4.7000    4.2000    3.0500    2.5000];
e_bk = [0.4330    0.4416    0.4031    0.5000    0.5099    0.4924    0.5315    0.5745    0.5196    0.5408    0.5431    0.5408    0.5916    0.5244    0.4500    0.4472];

y2 = signal - bk;
ey2 = sqrt(e_signal.^2+e_bk.^2);

offset = -3;
x2 = (-x2+30+20+12 + offset);

% Q2 fitting 
xft = [0.2190    0.1790    0.1390    0.0990    0.0890    0.0790    0.0690    0.0590    0.0540    0.0490    0.0440    0.0390    0.0340    0.0290    0.0190    0.0090];
fit_val = [ 3.7901    9.3852   13.8434   19.7452   20.5835   22.5630   27.9532   37.8257   43.6080   48.7367   51.7883   51.4616   46.4870   36.9429   13.9050    2.3606
];

subplot(3,1,1)
hold on
errorbar(x2,y2',ey2,'o','linewidth',2,'markersize',8,'displayname','N=2')
plot(xft*1e3,fit_val,'-o','markersize',4,'linewidth',1.5,'displayname','N=2 fitting')
box on
legend
xlim([0 300])
xlabel('532 center delay (ns)')
ylabel('KRb ion counts')
set(gcf,'color','white')
set(gca,'fontsize',16)


%% Q1 data: '20230514_100VR_30G_10kHz_REMPI_scan_2'
x1 = [-160  -120   -80   -40   -30   -20   -10     0     5    10    15    20    25    30    40    50];
signal = [4.0000    8.4000    7.6000   12.9333   13.3333   18.4667   21.0000   28.3333   29.4000   29.0000   33.3333   25.4000   29.0667   20.8667    9.2667    2.1333];
e_signal = [0.6037    0.8055    0.8165    0.9615    1.0022    1.1643    1.2275    1.4095    1.4376    1.4376    1.5261    1.3416    1.4329    1.2166    0.8300    0.4989];
bk = [2.2000    2.3333    2.3333    3.4000    2.8000    2.7333    3.3333    3.6667    4.8000    3.3333    4.1333    3.7333    2.2667    3.7333    2.8667    2.3333];
e_bk = [0.4944    0.4944    0.5617    0.5375    0.5497    0.5538    0.5735    0.5850    0.6532    0.5963    0.6182    0.5963    0.5164    0.5812    0.5121    0.5121];


y1 = signal - bk;
ey1 = sqrt(e_signal.^2+e_bk.^2);

offset = -3;

x1 = (-x1+30+20+12 + offset);

% Q1 fitting
xft = [ 0.2190    0.1790    0.1390    0.0990    0.0890    0.0790    0.0690    0.0590    0.0540    0.0490    0.0440    0.0390    0.0340    0.0290    0.0190    0.0090];
fit_val = [ 1.6103    4.1175    6.6344    9.8207   11.3804   14.0648   18.2303   23.2607   25.4733   26.9018   27.1456   25.6937   22.2698   17.1687    6.2317    1.0413];
subplot(3,1,2)
hold on
errorbar(x1,y1',ey1,'o','linewidth',2,'markersize',8,'displayname','N=1')
plot(xft*1e3,fit_val,'-o','markersize',4,'linewidth',1.5,'displayname','N=1 fitting')
box on
legend
xlim([0 300])
xlabel('532 center delay (ns)')
ylabel('KRb ion counts')
set(gcf,'color','white')
set(gca,'fontsize',16)

%% R0 data
%  '20230513_100VR_30G_10kHz_REMPI_scan_4'

x0 = [-220  -160  -150  -140   -90   -40     0    10    15    20    30    40    50 ];
signal = [1.9500   10.3684   14.4211   18.2632   16.7500   18.8000   18.4211   19.5500   18.2632   18.9474   21.0500   12.8947    3.0000   ];
e_signal = [ 0.4213    0.7572    0.9146    1.0083    0.9631    0.9975    1.0421    1.0404    1.0246    1.0367    1.0665    0.8823    0.5130    ];

bk = [1.7500    3.5263    4.7895    4.2105    3.7500    4.9000    4.5789    4.4500    5.0000    4.7368    3.8000    3.4737    1.3158    ];
e_bk = [0.4093    0.4618    0.5741    0.5263    0.5268    0.5477    0.5978    0.5723    0.5931    0.5717    0.5244    0.5316    0.4178  ];


y0 = signal - bk;
ey0 = sqrt(e_signal.^2+e_bk.^2);

offset = -3;
x0 = (-x0+30+20+12 + offset);
% 
% xft = [0.2790    0.2190    0.2090    0.1990    0.1490    0.0990    0.0590    0.0490    0.0440    0.0390    0.0290    0.0190    0.0090];
% fit_val = [ 0.0000    1.7079   10.6850   13.5881   13.8966   14.0849   14.2513   14.2818   14.3167   14.2911   14.1252    8.8378    0.9344];


xft = [0.0090    0.0190    0.0290    0.0390    0.0440    0.0490    0.0590    0.0990    0.1490    0.1990    0.2090    0.2190    0.2790];
fit_val = [1.4342    8.4460   15.1050   14.6694   14.8308   14.8318   14.4568   13.7614   12.8656   11.8952   10.1142    3.0224    0.0002];
subplot(3,1,3)
hold on
errorbar(x0,y0',ey0,'o','linewidth',2,'markersize',8,'displayname','N=0')
plot(xft*1e3,fit_val,'-o','markersize',4,'linewidth',1.5,'displayname','N=0 fitting')
box on
xlim([0 300])
legend
xlabel('532 center delay (ns)')
ylabel('KRb ion counts')
set(gcf,'color','white')
set(gca,'fontsize',16)